from django.shortcuts import render
from rest_framework.views import APIView,Response
from .sers import *


class Huilist(APIView):
    def post(self,request):
        user = User.objects.all()
        ser = UserSer(user,many=True)
        return Response(ser.data)

    def put(self,request):
        uid = request.data.get("uid")
        User.objects.filter(level=uid).update(level=2)
        Coupon.objects.all().update(user_id=uid)
        return Response({"msg":"升级成功"})


class Tushulist(APIView):
    def post(self,request):
        pro = Product.objects.all()
        ser = ProductSer(pro,many=True)
        return Response(ser.data)


class TushuLogin(APIView):
    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        user=User.objects.filter(username=username,password=password).first()
        if user:
            return Response({"msg":"登录成功","uid": user.id})
        else:
            return Response({"msg": "请重试"})


class ConponView(APIView):
    def get(self,request):
        uid=request.GET.get('uid')
        print(uid)
        queryset=Coupon.objects.filter(user_id=uid)
        ser=CouponSer(queryset,many=True)
        return Response(ser.data)